<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Information extends CI_Controller {

    public $session_data;

    public function __construct()
    {
        parent::__construct();
        $this->load->model('info_model');
        $this->load->helper(array('form', 'url'));

        $this->session_data = $this->session->userdata('logged_in');
    }

    public function view_user($user)
    {

        if ($this->session->userdata('logged_in'))
        {
            $this->check_user($user);

            $this->load->helper('form');

            $data['title'] = ucfirst('info');

            $result = $this->info_model->check_acc($user);

            $result_acc = $this->info_model->check_prof($user);
            $result2 = $this->info_model->check_college($result_acc -> coll_id);
            $result3 = $this->info_model->check_image($user);

            $data['test'] = $result;
            $data['owner_id'] = $this->session_data['user_id'];
            $data['fname'] = $result->fname;
            $data['lname'] = $result->lname;
            $data['middle_initial'] = $result->middle_initial;
            $data['gender'] = $result->gender;
            $data['birthday'] = $result->birthday;
            $data['address'] = $result->address;
            $data['email'] = $result->email;
            $data['bio'] = $result->bio;
            $data['user_id'] = $result->user_id;
            $session_data = $this->session->userdata('logged_in');
            $data['typeh'] = $session_data['type_no'];


            $data['coll_name'] = ucfirst($result2->coll_name);

            $data['image'] = $result3->img_name;

            $this->load->view('template/header', $data);
            $this->load->view('pages/info',$data);
            $this->load->view('template/footer');

        }
        else
            redirect('welcome/home','refresh');

    }

    public function edit_user($user)
    {

        if ($this->session->userdata('logged_in'))
        {

            $this->check_user($user);

            $result = $this->info_model->check_prof($user);

            $this->check_user_type($user, $result->type_no);

            $result = $this->info_model->check_acc($user);

            $this->load->helper('form');
            $this->load->library('form_validation');

            $data['owner_id'] = $this->session_data['user_id'];
            $data['title'] = "Edit Account";
            $data['fname'] = $result->fname;
            $data['lname'] = $result->lname;
            $data['middle_initial'] = $result->middle_initial;
            $data['gender'] = $result->gender;
            $data['birthday'] = $result->birthday;
            $data['address'] = $result->address;
            $data['email'] = $result->email;
            $data['bio'] = $result->bio;
            $data['user_id'] = $result->user_id;
            $data['error'] = "";
            $data['upload'] = "";
            $data['name'] = $result->fname. " " . $result->middle_initial . ". " . $result->lname;
            $user = $result->user_id;
            $session_data = $this->session->userdata('logged_in');
            $data['typeh'] = $session_data['type_no'];

            $this->form_validation->set_rules('fname', 'First Name', 'trim|required|xss_clean');
            $this->form_validation->set_rules('minitial', 'Middle Initial', 'trim|required|xss_clean');
            $this->form_validation->set_rules('lname', 'Last Name', 'trim|required|xss_clean');
            $this->form_validation->set_rules('address', 'Address', 'trim|required|xss_clean');
            $this->form_validation->set_rules('gender', 'Gender', 'trim|required|xss_clean');
            $this->form_validation->set_rules('birthday', 'Birthdate', 'trim|required|xss_clean');
            $this->form_validation->set_rules('bio', 'Biography', 'trim|required|xss_clean');

            if ($this->form_validation->run() === FALSE)
            {

                $this->load->view('template/header', $data);
                $this->load->view('pages/edit_info', $data);
                $this->load->view('template/footer');

            }
            else
            {

                $new = array(
                    "fname" => $this->input->post('fname'),
                    "middle_initial" => $this->input->post('minitial'),
                    "lname" => $this->input->post('lname'),
                    "address" => $this->input->post('address'),
                    "gender" => $this->input->post('gender'),
                    "birthday" => $this->input->post('birthday'),
                    "bio" => $this->input->post('bio')
                );

                $this->info_model->change_info($user, $new);

                redirect('information/edit_user/'.$user, 'refresh');

            }
        }
        else
            redirect('welcome/home','refresh');

    }

    public function edit_pass($user)
    {

        if ($this->session->userdata('logged_in'))
        {

            $this->check_user($user);

            $result = $this->info_model->check_prof($user);

            $this->check_user_type($user, $result->type_no);

            $result = $this->info_model->check_acc($user);

            $this->load->helper('form');
            $this->load->library('form_validation');
            $data['title'] = "Edit Account";
            $data['user_id'] = $result->user_id;
            $data['owner_id'] = $this->session_data['user_id'];
            $data['name'] = $result->fname. " " . $result->middle_initial . ". " . $result->lname;
            $user = $result->user_id;
            $session_data = $this->session->userdata('logged_in');
            $data['typeh'] = $session_data['type_no'];

            $this->form_validation->set_rules('old_password', 'Old Password', 'trim|required|xss_clean');
            $this->form_validation->set_rules('new_password', 'New Password', 'trim|required|xss_clean');
            $this->form_validation->set_rules('re-new', 'Re-new Password', 'trim|required|xss_clean|callback_check_password');

            if ($this->form_validation->run() === FALSE)
            {

                $this->load->view('template/header', $data);
                $this->load->view('pages/edit_pass', $data);
                $this->load->view('template/footer');

            }

            else
            {

                $new = $this->input->post('new_password');

                $this->info_model->change_pass($user, $new);

                redirect('information/edit_pass/'.$user, 'refresh');
            }
        }
        else
            redirect('welcome/home','refresh');

    }

    function check_password ($re_new)
    {

        $old = $this->input->post('old_password');
        $new = $this->input->post('new_password');
        $result = $this->info_model->check_pass($old);

        if($result)
        {

            if($new == $re_new)
                return TRUE;
            else
            {
                $this->form_validation->set_message('check_password', "New and re-new is not the same");
                return false;
            }

        }
        else
        {
            $this->form_validation->set_message('check_password', "Something's Wrong");
            return false;
        }

    }

    public function upload($user)
    {

        $config['upload_path'] = 'assets/images/profile';
        $config['allowed_types'] = 'gif|jpg|png';
        //$config['overwrite'] = 'TRUE';
        $config['encrypt_name'] = 'TRUE';
        $config['max_size']	= '2048';
        $config['max_width']  = '1600';
        $config['max_height']  = '900';

        $result = $this->info_model->check_acc($user);

        $this->load->helper('form');
        $this->load->library('form_validation');

        $data['title'] = "Edit Account";
        $data['fname'] = $result->fname;
        $data['lname'] = $result->lname;
        $data['middle_initial'] = $result->middle_initial;
        $data['gender'] = $result->gender;
        $data['birthday'] = $result->birthday;
        $data['address'] = $result->address;
        $data['email'] = $result->email;
        $data['bio'] = $result->bio;
        $data['user_id'] = $result->user_id;
        $data['error'] = "";
        $data['owner_id'] = $this->session_data['user_id'];
        $data['name'] = $result->fname. " " . $result->middle_initial . ". " . $result->lname;

        $file = 'u_file';


        $this->load->library('upload', $config);

        $session_data = $this->session->userdata('logged_in');

        if ( ! $this->upload->do_upload($file))
        {
            $data2 = array('error' => $this->upload->display_errors());

            redirect('information/edit_user/'.$user, 'refresh');

        }
        else
        {

            $data['upload'] = $this->upload->data();

            $this->info_model->change_image($user,$data['upload']);



            redirect('information/edit_user/'.$user, 'refresh');
        }

    }

    public function logout()
    {

        $this->session->unset_userdata('logged_in');
        redirect('welcome/home', 'refresh');

    }

    public function check_user_type($user, $rank){

        $result = $this->info_model->check_prof($user);

        $data = $this->session->userdata('logged_in');

        if(($data['type_no'] >= $rank && $result->user_id != $data['user_id']) && ($result->coll_id != $data['coll_id'] && $data['coll_id'] != 0))
        {

            redirect('information/view_user', 'refresh');

        }

    }

    public function check_user($user){

        $session_data = $this->session->userdata('logged_in');

        $result = $this->info_model->check_user($user);

        $type = $session_data['type_no'];
        $user_id = $session_data['user_id'];
        $coll_id = $session_data['coll_id'];

        if( ($type == 1 and $coll_id != $result['coll_id']) or ($type > 1 and $coll_id != $result['coll_id']) )
            redirect('information/view_user/'.$user_id, 'refresh');

        if(empty($result))
            redirect('information/view_user/' . $user_id, 'refresh');

        //echo var_dump($session_data);
        //echo var_dump($result);
        //echo var_dump($result['coll_id'] == $session_data['coll_id']);

    }

}

/* End of file information.php */
/* Location: ./application/controllers/information.php */